// pages/index/index.js
const app = getApp()
const { get } = require('../../utils/request')
const { updateCartBadge } = require('../../utils/util')

Page({
  data: {
    banners: [], // 轮播图
    categories: [], // 分类导航
    recommendGoods: [], // 推荐商品
    hotGoods: [], // 热销商品
    loading: true,
    hasMore: true,
    page: 1,
    pageSize: 10
  },
  
  onLoad() {
    this.getHomeData()
    updateCartBadge()
  },
  
  onShow() {
    // 每次显示页面时更新购物车数量
    updateCartBadge()
  },
  
  /**
   * 获取首页数据
   */
  getHomeData() {
    Promise.all([
      this.getBanners(),
      this.getCategories(),
      // this.getRecommendGoods(),
      // this.getHotGoods()
    ]).then(() => {
      this.setData({ loading: false })
    }).catch(err => {
      console.error('首页数据获取失败:', err)
      this.setData({ loading: false })
    })
  },
  
  /**
   * 获取轮播图
   */
  getBanners() {
    // return get('/banners').then(res => {
    //   this.setData({ banners: res })
    // })
    // todo 测试阶段 使用测试数据
    return this.setData({
      banners: [
        {imageUrl:'/images/test-img/longbo1.jpg'},
        {imageUrl:'/images/test-img/longbo2.jpg'},
        {imageUrl:'/images/test-img/longbo3.jpg'}
      ]
    });
  },
  
  /**
   * 获取分类导航
   */
  // getCategories() {
  //   return get('/categories').then(res => {
  //     this.setData({ categories: res })
  //   })
  // },
  getCategories(){
    this.setData({ categories: [
      // 测试原有数据...
      {
        id: 2,
        iconUrl:'/images/categories/生鲜.png',
        name: '生鲜'
      },{
        id: 3,
        iconUrl:'/images/categories/零食.png',
        name: '零食'
      },{
        id: 4,
        iconUrl:'/images/categories/日用品.png',
        name: '日用品'
      },{
        id: 5,
        iconUrl:'/images/categories/购物车.png',
        name: '购物车'
      },{
        id: 6,
        iconUrl:'/images/categories/热销.png',
        name: '热销'
      }
    ] })
  },
  
  /**
   * 获取推荐商品
   */
  getRecommendGoods() {
    return get('/goods/recommend', {
      page: this.data.page,
      pageSize: this.data.pageSize
    }).then(res => {
      const { list, hasMore } = res
      this.setData({
        recommendGoods: [...this.data.recommendGoods, ...list],
        hasMore,
        page: this.data.page + 1
      })
    })
  },
  
  /**
   * 获取热销商品
   */
  getHotGoods() {
    return get('/goods/hot', { limit: 10 }).then(res => {
      this.setData({ hotGoods: res })
    })
  },
  
  /**
   * 跳转到商品详情页
   */
  goGoodsDetail(e) {
    const { id } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/goods/detail?id=${id}`
    })
  },
  
  /**
   * 跳转到分类页
   */
  goCategory(e) {
    const { id } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/category/list?id=${id}`
    })
  },
  
  /**
   * 搜索商品
   */
  onSearch() {
    wx.navigateTo({
      url: '/pages/search/search'
    })
  },
  
  /**
   * 下拉刷新
   */
  onPullDownRefresh() {
    this.setData({
      banners: [],
      categories: [],
      recommendGoods: [],
      hotGoods: [],
      loading: true,
      hasMore: true,
      page: 1
    })
    
    this.getHomeData().then(() => {
      wx.stopPullDownRefresh()
    })
  },
  
  /**
   * 上拉加载更多
   */
  onReachBottom() {
    if (this.data.hasMore && !this.data.loading) {
      this.getRecommendGoods()
    }
  }
})    